<?php
namespace app\index\model;
use app\common\model\BaseModel;
/**
 * 权限规则model
 */
class AuthGroupAccessModel extends BaseModel{
    protected $table = 'think_auth_group_access';
	/**
	 * 根据group_id获取全部用户id
	 * @param  int $group_id 用户组id
	 * @return array         用户数组
	 */
	public function getUidsByGroupId($group_id){
		$user_ids=$this
			->where(array('group_id'=>$group_id))
			->field('uid')
            ->select();
		$data = array();
		foreach ($user_ids as $k => $v){
		    array_push($data,$v['uid']);
        }
		return $data;
	}

	/**
	 * 获取管理员权限列表
	 */
	public function getAllData(){
		$data=$this
			->field('u.id,u.status,u.username,u.password,u.phone,u.email,aga.group_id,ag.title')
			->alias('aga')
			->join('think_users u', 'aga.uid=u.id','RIGHT')
			->join('think_auth_group ag','aga.group_id=ag.id','LEFT')
			->select();

		$user_data = array();
        $uids = array();

        // 组合数组
		foreach ($data as $k => $v) {
            if (!in_array($v['id'], $uids)) {
                array_push($uids,$v['id']);
                array_push($user_data,$data[$k]);
            }else{
                foreach ($user_data as $k1 => $v1){
                    if($v1['id'] == $v['id'] ){
                        $user_data[$k1]['title'] .= ','.$v['title'];
                        $user_data[$k1]['group_id'] .= ','.$v['group_id'];
                    }
                }
            }
		}

		// 管理组title数组用顿号连接
		return $user_data;

	}


}
